<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <!-- 验证表单的必填项
          :model="formData"  表单收集数据对象
          :rules="formFules" 验证规则对象
          ref="formRef" 兜底校验  校验所有表单规则对象
       -->
      <el-form :model="formData" :rules="formFules"  ref="formRef" style="padding:0 25px">
        <!-- prop="username" 指向具体的验证规则项 -->
        <el-form-item prop="username">
          <el-input v-model="formData.username" prefix-icon="el-icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="formData.password" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input v-model="formData.repassword" prefix-icon="el-icon-lock" placeholder="请确认密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="btnReg" style="width:100%" type="primary">注册</el-button>
          <el-link  type="info" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Reg",
  data(){
    //自定义校验规则函数
    var rePwd = (rule, value, callback) => {
      // 表示当前表单项的值
      //callback 回调函数
      //如果校验失败 使用callback 传入一个错误对象
      //如果校验成功使用callback 不传入任何内容,放心
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.formData.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      formData:{     //表单收集数据对象
        username:'',
        password:'',
        repassword:''
      },
      formFules:{    //验证规则对象
        username:[
          { required: true, message: '请输入用户名', trigger: 'blur' },  //必填校验
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '请输入1~10字母和数字', trigger: 'blur' },//正则校验
        ],
        password:[
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '请输入6~15位密码', trigger: 'blur' },
        ],
        repassword:[
          { pattern: /^\S{6,15}$/, message: '请输入6~15位密码', trigger: 'blur' },
          { validator: rePwd, trigger: 'blur' }
        ],
      }
    }
  },
  methods:{
    btnReg(){
      //兜底校验
      this.$refs.formRef.validate( async (valid) =>{
        if (!valid) return console.log(res.message);
        //3发送请求 实现注册
        const {data:res} = await this.$http.post('/api/reg',this.formData)
        //3.1注册失败 提示用户
        if(res.code !== 0) return  this.$message.error(res.message);
        //3.2注册成功 提示用户
        this.$message.success(res.message);
        //4.跳转到登录页
        this.$router.push('/Login')       
      })
    }
  }
};
</script>

<style lang="less" scoped>
.reg-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }
  }
}
</style>
